<template>
  <div class="register">
    <el-form ref="registerForm" :model="form" :rules="rules" class="register-form">
      <h2>用户注册</h2>
      <el-form-item prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱">
        </el-input>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item prop="userName">
        <el-input v-model="form.userName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="nickname">
        <el-input v-model="form.nickname" placeholder="请输入用户昵称"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input type="password" v-model="form.confirmPassword" placeholder="请再次输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">注册</el-button>
        <el-button @click="registerForm.resetFields()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <el-dialog v-model="codeDialog" title="请激活邮箱">
    <el-row>
      <el-input v-model="cEmail" placeholder="邮箱" />
    </el-row>
    <el-row :gutter="20" style="margin-top: 1rem;">
      <el-col :span="20"><el-input v-model="code" placeholder="验证码" />
      </el-col>
      <el-col :span="4"><el-button @click="sendCode">发送验证码</el-button></el-col>
    </el-row>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="verifyCode">验证</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { getEmailCodeAPI, verifyEmailAPI, registerAPI, enableUserAPI } from '@/apis/user';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

const router = useRouter();
const registerForm = ref(null);
const codeDialog = ref(false);
const code = ref('');
const cEmail = ref('');

const sendCode = () => {
  const em = cEmail.value;
  const uname = form.nickname;
  // 验证验证码
  getEmailCodeAPI({
    email: em,
    name: uname
  }).then(res => {
    ElMessage.success('验证码已发送');
  })
}

const verifyCode = () => {
  const e = cEmail.value;
  const c = code.value;
  // 验证验证码
  verifyEmailAPI(e, c).then(res => {
    if (res.success === true) {
      
      enableUserAPI(e).then(res=>{
        codeDialog.value = false;
        router.push('/login');
        ElMessage.success(res.message);
      }).catch(err=>{
        ElMessage.error(err.message);
      })
      
    } else {
      // 验证失败后，显示错误提示
      ElMessage.error('验证码错误');
    }

  })
}

const form = reactive({
  email: '',
  phone: '',
  userName: '',
  nickname: '',
  password: '',
  confirmPassword: '',
});

const rules = reactive({
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  nickname: [
    { required: true, message: '请输入用户昵称', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== form.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ],
});

const submitForm = () => {
  registerForm.value.validate((valid) => {
    if (valid) {
      const { email, phone, userName, nickname, password } = form;
      // 发送注册请求
      registerAPI({
        email,
        phone,
        userName,
        nickname,
        password
      }).then(res => {
        if(res.status ===500){
          ElMessage.error(res.message);
          return;
        }
        // 注册成功后，显示验证码弹窗
        codeDialog.value = true;
        cEmail.value = email;
      })

    } else {
      ElMessage.error('请完善信息');
      return false;
    }
  });
};
</script>

<style lang="scss" scoped>
.register {
  width: 100%;
  height: 100%;

  .register-form {
    width: 400px;
    margin: 0 auto;
    margin-top: 100px;
    background-color: #fff;
    border-radius: 5px;
    padding: 20px;

    .el-button {
      width: 48%;
    }
  }
}
</style>
